package com.gupt.weekly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gupt.weekly.dao.UserMapper;
import com.gupt.weekly.domain.User;
import com.gupt.weekly.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author linzizun
 * @version 1.0
 * @date 2021/03/09 16:51
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int hasUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return null == userMapper.selectOne(queryWrapper) ? 0 : 1;
    }

    @Override
    public User selectByUsernameAndPassword(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername())
                .eq("password", user.getPassword());
        return userMapper.selectOne(queryWrapper);
    }


    @Override
    public User selectById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> selectByCondition(String condition) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", condition)
                .or().like("name", condition)
                .or().like("phone", condition);
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public int updateInfoById(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int updatePasswordById(User user, String new_password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername())
                .eq("password", user.getPassword());
        user = userMapper.selectOne(queryWrapper);
        if (null != user) {
            user.setPassword(new_password);
            return userMapper.updateById(user);
        }
        return 0;
    }
}
